<template>
  <div class="item">

    <div class="item-search">
      <el-collapse>
        <el-collapse-item name="1">
          <template slot="title">
            <el-button type="primary">搜索</el-button>
          </template>
          <div class="item-search-input">
            <el-row>
              <el-col :span="8">
                <el-date-picker class="item-search-input-l" @keyup.enter.native="search" v-model="dateTimeScope" type="datetimerange"  placeholder="搜索时间范围" align="left" :editable="false" @input="dateTimeScopeChange"></el-date-picker>
              </el-col>
              <el-col :span="1">
                <el-button type="primary" icon="search" @click="search"></el-button>
              </el-col>
              <el-col :span="1">
                <el-button type="primary" icon="delete" @click="reset"></el-button>
              </el-col>
            </el-row>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>

    <div class="item-table">

      <div class="item-table-title">
        <h2>{{ tableTitle.big }}<span>{{ tableTitle.small }}</span></h2>
        <div class="item-table-add">
        </div>
      </div>

      <div class="item-table-main">

        <div id="myPie" style="width: 100%;height: 420px;"></div>

      </div>

    </div>

  </div>
</template>

<script type="text/ecmascript-6">
  import { mapState } from 'vuex'
  import ajax from '../../../utils/ajax/index'
  import setDateTime from '../../../utils/date/setDateTime'
  import Highcharts from 'Highcharts'
  export default {
    data () {
      return {
        dateTimeScope: [],
        searchData: {
          startTime: '', endTime: ''
        }
      }
    },
    computed: mapState({
      tableTitle: state => state.tableTitle
    }),
    mounted () {
      this.Result()
    },
    methods: {
      Result (formData) {
        let that = this
        let store = that.$store
        let url = that.$api.payTradeDealTotal.result

        ajax.get(url, formData || {}, store, true).then((res) => {
          let data = res.data
          that.Draw(data)
        })
      },
      search () {
        this.Result(this.searchData)
      },
      reset () { // 清空
        this.dateTimeScope = []
        this.$clearJson(this.searchData)
      },
      Draw (changeData) {
        let myPie
        let option = {
          credits: {
            enabled: false
          },
          chart: {
            type: 'pie',
            options3d: {
              enabled: true,
              alpha: 45,
              beta: 0
            }
          },
          title: false,
          tooltip: {
            headerFormat: '{series.name}<br>',
            pointFormat: '<b>{point.name}</b>：{point.y}人，占比：{point.percentage:.1f}%'
          },
          plotOptions: {
            pie: {
              allowPointSelect: true,
              cursor: 'pointer',
              depth: 35,
              dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>：{point.y}人，占比：{point.percentage:.1f}%'
              },
              showInLegend: true
            }
          },
          series: [{
            type: 'pie',
            name: '刷码人数',
            data: [
              { y: parseInt(changeData[0]) + parseInt(changeData[3]), name: '第2次换乘优惠' },
              { y: parseInt(changeData[1]), name: '第3次换乘优惠' },
              { y: parseInt(changeData[2]), name: '第4次换乘优惠' },
              { y: parseInt(changeData[4]), name: '未使用换乘优惠' }
            ]
          }]
        }
        myPie = new Highcharts.Chart('myPie', option)
        console.log(myPie)
      },
      dateTimeScopeChange (val) {
        this.searchData.startTime = setDateTime(val[0])
        this.searchData.endTime = setDateTime(val[1])
      }
    }
  }
</script>
